#include "fldrv_board.h"
#include "flapp_bat_manage.h"
#include "flapp_system.h"
#include "fldrv_adc.h"
#include "flapp_peripheral.h"
#include "flapp_net_slm.h"

__IO u8 dma_trans_complete_flag;
extern uint16_t adc1_valuetab[ADC_CH_MAX];

typedef struct 
{			
	bool fRefreshEle;	//发送标志																		
}flapp_bat_para_t;	//通讯获取到的参数存储，有需要就获取

flapp_bat_para_t bat_para;

const u16 tblChgEle[] = {
	3700,			// 0%
	3747,			// 10%
	3793,			// 20%
	3840,			// 30%
	3887,			// 40%
	3933,			// 50%
	3980,			// 60%
	4027,			// 70%
	4073,			// 80%
	4120,			// 90%
	4150,			//100%
};

const u16 tblDisEle[] = {
	3200,			// 0%
	3360,			// 10%
	3400,			// 20%
	3500,			// 30%
	3600,			// 40%
	3680,			// 50%
	3750,			// 60%
	3800,			// 70%
	3850,			// 80%
	3900,			// 90%
	3980,			//100%
};

void fldrv_bat_manage_init(void)
{
	bat_para.fRefreshEle = TRUE;
	fldrv_read_bat_adc_init();
}

static void adc_get_charge_ele(u16 val)
{
	u8 i;
	flapp_sys_para_t sys_bat_para;
	sys_bat_para = flapp_get_sys_para();
	if(bat_para.fRefreshEle)
	{
		bat_para.fRefreshEle = FALSE;
		for(i = BAT_POW_OFF;i < BAT_ELE_FULL;i++)			
		{
			if(val < tblChgEle[i])
				break;
		}
		sys_bat_para.u8BatEle  =  i;
		flapp_set_sys_para(sys_bat_para);
	}
	else
	{
		if(val >= tblChgEle[sys_bat_para.u8BatEle] && sys_bat_para.u8BatEle < 10)
		{
			if(++sys_bat_para.u8BatEle <= BAT_ELE_FULL)
			{
				sys_bat_para.u8BatEle = BAT_ELE_FULL;
				flapp_set_sys_para(sys_bat_para);
			}
		}
	}
	
}

static void adc_get_dis_ele(u16 val)
{
	u8 i;
	flapp_sys_para_t sys_bat_para;
	sys_bat_para = flapp_get_sys_para();
	if(bat_para.fRefreshEle)
	{
		bat_para.fRefreshEle = 0;
		for(i = BAT_POW_OFF;i < BAT_ELE_FULL;i++)
		{
			if(val < tblDisEle[i])
				break;
		}
		sys_bat_para.u8BatEle = i;
		flapp_set_sys_para(sys_bat_para);
	}
	else
	{
		if(val < tblDisEle[sys_bat_para.u8BatEle] && sys_bat_para.u8BatEle <= BAT_ELE_FULL)
		{
			if(sys_bat_para.u8BatEle)
			{
				sys_bat_para.u8BatEle--;
				flapp_set_sys_para(sys_bat_para);
			}
			if((sys_bat_para.u8BatEle <= BAT_POW_OFF) && (sys_bat_para.bPowOnOff == ON))
			{
				flapp_device_power_off();
				flapp_set_http_req(API_RPT_DEV_PARA,API_LOW_POW_POWER_OFF);
			}
		}
	}
}
/**
* @brief  电源管理
  * @param  void
  * @retval none
  */
void flapp_bat_manage_run(void)
{
	u16 fBatVol;
	static u16 u16VrefAvg = 0;
	static u16 u16VrefMax = 0;
	static u16 u16VrefMin = 0x0fff;
	static u16 u16BatAvg = 0;
	static u16 u16BatMax = 0;
	static u16 u16BatMin = 0x0fff;
	static u8 cAdcCnt = 0;
	flapp_sys_para_t sys_part;
	sys_part = flapp_get_sys_para();
	
	if(sys_part.bPowOnOff == OFF)
	{
		u16VrefAvg = 0;
		u16VrefMax = 0;
		u16VrefMin = 0x0fff;
		u16BatAvg = 0;
		u16BatMax = 0;
		u16BatMin = 0x0fff;
		cAdcCnt = 0;
		dma_trans_complete_flag = FALSE;
		return;
	}
	
	if(dma_trans_complete_flag >= ADC_CH_MAX)
	{
		dma_trans_complete_flag = FALSE;
		u16VrefAvg += adc1_valuetab[ADC_CH0_VER];
		if(adc1_valuetab[ADC_CH0_VER] > u16VrefMax)	u16VrefMax = adc1_valuetab[ADC_CH0_VER];
		if(adc1_valuetab[ADC_CH0_VER] < u16VrefMin)	u16VrefMin = adc1_valuetab[ADC_CH0_VER];
		
		u16BatAvg += adc1_valuetab[ADC_CH1_BAT];
		if(adc1_valuetab[ADC_CH1_BAT] > u16BatMax)	u16BatMax = adc1_valuetab[ADC_CH1_BAT];
		if(adc1_valuetab[ADC_CH1_BAT] < u16BatMin)	u16BatMin = adc1_valuetab[ADC_CH1_BAT];
		
		if(++cAdcCnt >= ADC_READ_CNT)
		{
			u16VrefAvg -= u16VrefMax;
			u16VrefAvg -= u16VrefMin;
			u16VrefAvg >>= 3;
			
			u16BatAvg -= u16BatMax;
			u16BatAvg -= u16BatMin;
			u16BatAvg >>= 2;
			
			fBatVol = 1200*u16BatAvg/u16VrefAvg;
			
			if(sys_part.bHaveUsb)
			{
				adc_get_charge_ele(fBatVol);
				if(fBatVol >= tblChgEle[BAT_ELE_80])
					EN_CHAR_ASSI_ON;
			}
			else
			{
				adc_get_dis_ele(fBatVol);
			}
			//log_debug("bat_val:%d\t%d\r\n",fBatVol,sys_part.bHaveUsb);
			u16VrefAvg = 0;
			u16VrefMax = 0;
			u16VrefMin = 0x0fff;
			u16BatAvg = 0;
			u16BatMax = 0;
			u16BatMin = 0x0fff;
			cAdcCnt = 0;
		}
	}
}

void flapp_set_bat_refresh(bool flag)
{
	bat_para.fRefreshEle = flag;
}

